package parser

import (
	"crawler/engine"
	"regexp"
	"strings"
)

var (
	profileRe = regexp.MustCompile(`<a href="(http://album.zhenai.com/u/[0-9]+)"[^>]*>([^<]+)</a>`)
	cityUrlRe = regexp.MustCompile(`href="(http://www.zhenai.com/zhenghun/[^"]+)"`)
)

// 城市解析器
func ParseCity(contents []byte) engine.ParserResult {
	matches := profileRe.FindAllSubmatch(contents, -1)

	result := engine.ParserResult{}
	for _, match := range matches {
		name := string(match[2])
		//result.Items = append(result.Items, "User "+name)

		result.Requests = append(result.Requests, engine.Request{
			Url: strings.Replace(string(match[1]), `\u002F`, "/", -1),
			ParserFunc: func(c []byte) engine.ParserResult {
				return ParseProfile(c, name)
			},
		})
	}

	//解析更多城市
	urlMatches := cityUrlRe.FindAllSubmatch(contents, -1)
	for _, match := range urlMatches {
		result.Requests = append(result.Requests, engine.Request{
			Url:        strings.Replace(string(match[1]), `\u002F`, "/", -1),
			ParserFunc: ParseCity,
		})
	}

	return result
}
